package com.ontheroad.mysql.news.service;

import cn.modoumama.service.base.impl.BaseServiceImpl;
import com.ontheroad.api.request.ActivityRequest;
import com.ontheroad.api.response.Response;
import com.ontheroad.core.util.DateUtils;
import com.ontheroad.core.util.ThreadPool;
import com.ontheroad.leanCloud.model.PushType;
import com.ontheroad.leanCloud.service.LeanCloudService;
import com.ontheroad.mysql.news.mapper.NewsMapper;
import com.ontheroad.news.model.NewsModel;
import com.ontheroad.news.service.NewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by kedong on 2017/12/9 0009.
 */
@Service
@Transactional
public class NewsServiceImpl extends BaseServiceImpl<NewsModel, Integer> implements NewsService {

    @Autowired
    private NewsMapper mapper;

    @Autowired
    private LeanCloudService leanCloudService;

    @Autowired
    private ThreadPool threadPool;

    @Autowired
    public void setMapper(NewsMapper mapper) {
        setGenericMapper(mapper);
    }

    @Override
    public void insert(NewsModel entity) {
        mapper.insertModel(entity);
        push(entity);
    }

    @Override
    public int updateById(NewsModel entity) {
        int count = mapper.updateModelById(entity);
        push(entity);
        return count;
    }

    @Override
    public void push(final NewsModel model) {
        if (model != null) {
            final Map<String, Object> data = new HashMap<>();
            String images = model.getTitleImages();
            String img = "";
            if (StringUtils.isNotBlank(images)) {
                if (images.contains(",")) {
                    img = images.split(",")[0];
                } else {
                    img = images;
                }
            }
            //图片
            data.put("img", img);
            //时间
            data.put("time", DateUtils.dateToTimestamp(null));
            //1、文章，2、公告
            data.put("type", model.getType());
            //资讯ID
            data.put("id", model.getId());
            //详情url
            data.put("url", model.getUrl());
            //资讯标题
            data.put("title", model.getTitle());
            //资讯摘要
            data.put("intro", model.getIntro());
            //通知类型
            data.put("action", PushType.NEWS.getValue());
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        leanCloudService.push(model.getTitle(), null, data);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public Response list(ActivityRequest request) throws Exception {
        return null;
    }

    @Override
    public NewsModel save(NewsModel model) {
        mapper.insertModel(model);
        return model;
    }
}
